﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace iLangl.StateMachine
{
	public class StateMachineConfiguration : ConfigurationSection
	{
		private static ConfigurationProperty stateMachineFileNameProperty;

		private static StateMachineConfiguration instance;
		private static ConfigurationPropertyCollection properties;

		static StateMachineConfiguration()
		{
			stateMachineFileNameProperty = new ConfigurationProperty(
			  "stateMachineFileName",
			  typeof(String),
			  null,
			  ConfigurationPropertyOptions.IsRequired);

			properties = new ConfigurationPropertyCollection();
			properties.Add(stateMachineFileNameProperty);
		}

		private static object mutex = new object();
		/// <summary>
		/// The instance of the StateMachineConfiguration.
		/// </summary>
		public static StateMachineConfiguration Instance
		{
			get
			{
				if (instance == null)
				{
					lock (mutex)
					{
						if (instance == null)
						{
							instance = ConfigurationManager.GetSection("StateMachineConfiguration") as StateMachineConfiguration;
						}
					}
				}
				return instance;
			}
		}

		private String stateMachineFileName = String.Empty;
		/// <summary>
		/// Path to the filename with state machine description
		/// </summary>
		public String StateMachineFileName
		{
			get
			{
				if (!String.IsNullOrEmpty(stateMachineFileName))
				{
					return stateMachineFileName;
				}

				stateMachineFileName = base[stateMachineFileNameProperty] as String;

				return stateMachineFileName;
			}
		}

		public void SetStateMachineFileName(string filepath)
		{
			stateMachineFileName = filepath;
		}

		protected override ConfigurationPropertyCollection Properties
		{
			get
			{
				return properties;
			}
		}
	}
}
